function msg = f5d_fix(img, k, seed)
    % F5D  Extract message using F5.
    % MSG = F5D(IMG, K, SEED) extracts message from IMG.
    % It automatically calls BINARYTOARRAY to get the encoded message,
    % then xor random binary chars with it to get the original one.
    % K is the parameter in F5, and SEED is used for generating pesudo 
    % random sequences.

    if k <= 0
        error('k must be above zero.');
    end
    n = 2^k - 1;

    % ROWS = size(img,1)/8;
    r = int16(im2vec(img, [8 8])');    
    [M,N] = size(r);
    bin = zeros(1,M*N+k);
    % generate pusedo random sequence.
    if nargin < 3
        seed = M*N;
    end
    randstream = getRandStream(seed);
    tran = prand(M*N, randstream);
    
    % get all LSB
    count = 1;
    p = 0;
    usedval = ones(1,n) * -1;
    usedlen = 1;
    for i1 = 1:M
        for j1 = 1:N
            p = p + 1;
            t = tran(1,p);% t=1..N*M
            i = floor((t-1) / N)+1;
            j = (t-1) - (i-1)*N + 1;
            if j == 1
                continue;
            end
            u = r(i, j);
            if u == 0
                continue
            end
            usedval(1, usedlen) = u;
            usedlen = usedlen + 1;
            if usedlen > n
                v = f5_getValueAsF4(usedval, k, n);
                bin(1, count:count+k-1) = mod(v + round(randstream.rand(1,k)), 2);
                usedval = ones(1,n) * -1;
                usedlen = 1;
                count = count + k;
            end
        end
    end
    
    count = count - 1;
    
    % output
    msg = binaryToArray(bin, count);
    fprintf(1,'message is: %s\n', msg);
end
